﻿using System;
using System.ComponentModel.DataAnnotations;

namespace AdventureWorks.Entities
{
	public class Product : Entity
	{
		[Column("ProductID")]
		public override Int32 ID { get; set; }

		[Display(Name = "Product Name")]
		[Required]
		[MaxLength(50)]
		public String Name { get; set; }

		[Display(Name = "Product Number")]
		[Required]
		[MaxLength(25)]
		public String ProductNumber { get; set; }

		[MaxLength(15)]
		public String Color { get; set; }

		[Display(Name = "Standard Price")]
		[Required]
		public Decimal StandardCost { get; set; }

		[Display(Name = "List Price")]
		[Required]
		public Decimal ListPrice { get; set; }

		[Display(Name = "Size")]
		[Required]
		[MaxLength(5)]
		public String Size { get; set; }

		public decimal? Weight { get; set; }

		[Display(Name = "Sell Start Date")]
		[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
		[Required]
		public DateTime SellStartDate { get; set; }

		[Display(Name = "Sell End Date")]
		[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
		public DateTime? SellEndDate { get; set; }

		[Display(Name = "Discontinued Date")]
		[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
		public DateTime? DiscontinuedDate { get; set; }

		[Display(Name = "ThumbNail Photo")]
		public Byte[] ThumbNailPhoto { get; set; }

		[NotMapped]
		public String ThumbNailPhotoBase64 { 
			get { 
				if(ThumbNailPhoto != null)
					return Convert.ToBase64String(ThumbNailPhoto);
				else{
					return "";
				}
			}
			private set { }
		}

		[Display(Name = "Thumbnail Photo FileName")]
		public String ThumbnailPhotoFileName { get; set; }

		[Display(Name = "Modified Date")]
		[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
		public override DateTime ModifiedDate { get; set; }


		public virtual int ProductCategoryID { get; set; }
		public virtual ProductCategory ProductCategory { get; set; }

		public virtual int ProductModelID { get; set; }
		public virtual ProductModel ProductModel { get; set; }
		
		public override Boolean Validate()
		{
			if (String.IsNullOrEmpty(Name))
				ThrowInvalidEntityException(ExceptionMessages.NameInvalid);

			if (String.IsNullOrEmpty(ProductNumber))
				ThrowInvalidEntityException(ExceptionMessages.ProdNumberInvalid);

			if (StandardCost == 0)
				ThrowInvalidEntityException(ExceptionMessages.CostInvalid);

			if (ListPrice == 0)
				ThrowInvalidEntityException(ExceptionMessages.PriceInvalid);

			if (ProductCategory == null)
				ThrowInvalidEntityException(ExceptionMessages.CatInvalid);

			if (ProductModel == null)
				ThrowInvalidEntityException(ExceptionMessages.ModelInvalid);

			if (SellStartDate == DateTime.MinValue)
				ThrowInvalidEntityException(ExceptionMessages.StartDateInvalid);

			if (SellStartDate > SellEndDate)
				ThrowInvalidEntityException(ExceptionMessages.StartEndInvalid);

			if (ModifiedDate == DateTime.MinValue)
				ModifiedDate = DateTime.Now;

			return true;
		}

		public override string ToString()
		{
			return Name;
		}
	}
}